<?php
	add_action('widgets_init', 'register_relate_store_widget');

	function register_relate_store_widget()
	{
		register_widget('relate_store');
	}
	class relate_store extends WP_Widget
	{
        
		function relate_store()
		{
			$widget_ops = array('classname' => 'relate-store-w', 'description' =>
					'Display related stores - Store detail page');
			$control_ops = array(
				'width' => 250,
				'height' => 350,
				'id_base' => 'relate_store');
			$this->WP_Widget('relate_store', 'CouponX - Related stores', $widget_ops, $control_ops);
		}
		function widget($args, $instance) // display widget
		{
            extract($args);
			global $post;
			$store_id = $post->ID;

			$title = apply_filters('widget_title', $instance['title']);
            $kw = $instance['kw'];
            if($kw)
                $kw = ' '.$kw;
            $show_as = $instance['show_as'];
            $show_at = $instance['show_at'];
			$itemsToShow = $instance['number_show'];
			if (!$itemsToShow)
				$itemsToShow = 5;
                
            $related_items = cpx_get_relate_store($store_id, $itemsToShow);
// Show at right side bar
            if($show_at == 'store_sidebar') {
                if (count($related_items) > 0) { ?>
                <div class="row-fluid topstoresxx">
                    <div class="titlestopxx"><?php echo $title; ?></div>
                <?php
                    // Show store link 
                    if($show_as == 'link'): ?>
                    <ul class="peoplewho">
                    <?php
                    foreach ($related_items as $id) { ?>
                        <li><a href="<?php echo get_permalink($id); ?>" title="Get <?php echo get_post_field('post_title', $id).$kw; ?>"><?php echo get_post_field('post_title', $id).$kw; ?></a></li>
                    <?php } ?>
                        </ul>
                <?php
                    // Show store logo 
                    else: 
                        foreach ($related_items as $id) {
                            $st_home_url = get_post_meta($id, 'url_store_metadata', true);
                            $src_img = get_post_meta($id, 'logo_metadata', true);
                            if(!$src_img)
                                $src_img = cpx_get_store_thumb($st_home_url, 100, 50);
                ?>
                        <div class="group-top-stores">
                            <div class="top-store">
                                <a href="<?php echo get_permalink($id);?>" title="Get <?php echo get_post_field('post_title',$id) . $kw; ?>"><img src="<?php echo $src_img; ?>" width="50px" height="50px" /></a>
                            </div>
                        <span><a href="<?php echo get_permalink($id);?>" title="Get <?php echo get_post_field('post_title',$id) . $kw; ?>"><?php echo get_post_field('post_title', $id) . $kw; ?></a></span>
                        </div>
                <?php }
                    endif; ?>
                </div>
                <?php
                }
            }
// Show at store detail content
            else if($show_at == 'store_content')
            {
                $store_name = get_post_field('post_title', $store_id);
                $title = "People Who Liked $store_name Also Liked";
                
                $related_st_ids = cpx_get_relate_store($store_id, $itemsToShow);
                if(count($related_st_ids) > 0)
                { ?>
                    <div class="row-fluid topstoresxx">
                    <div class="relate-coupon"><h2><?php echo $title; ?></h2></div>
                <?php
                    foreach ($related_st_ids as $st_id) {
                        $arr_cp_id = cpx_get_lastest_cp(1, '', $st_id);
                        // If get 1 coupon on 1 store                        
                        cpx_get_coupon_detail_2($arr_cp_id[0], $kw);
                    } ?>
                    </div>
                <?php    
                }
            }
            
            // Reset Post Data
            wp_reset_postdata();
		}

		function update($new_instance, $old_instance) // update widget
		{
			$instance = $old_instance;
			$instance['title'] = $new_instance['title'];
            $instance['kw'] = $new_instance['kw'];
            $instance['show_as'] = $new_instance['show_as'];
			$instance['number_show'] = $new_instance['number_show'];
            $instance['show_at'] = $new_instance['show_at'];
			return $instance;
		}

    	function form($instance) // form for the widget options
    	{
    	   $arr_show_at = array('store_sidebar'=>'Store Detail right side bar','store_content'=>'Under Store Content');
           $arr_show_as = array('link' => 'Links','image' => 'Stores image');
?>
        <div style="color: #333;">
    		<p>        
    			<label for="<?php echo $this->get_field_id('title');?>"><?php echo 'Title'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php	echo $this->get_field_name('title');?>" value="<?php echo $instance['title'];?>" style="width:100%;" />
    		</p>
            <p>        
    			<label for="<?php echo $this->get_field_id('show_as');?>"><?php echo 'Show as (if show at store detail side bar)'; ?></label>
    			<select name="<?php echo $this->get_field_name('show_as'); ?>" id="<?php echo $this->get_field_id('show_as') ?>" style="width: 60%;">
                <?php  
                foreach ($arr_show_as as $k => $v) {
                    if ($instance['show_as'] == $k)
                        $selected = 'selected';
                    else
                        $selected = '';
                    echo "<option value='{$k}' {$selected}>$v</option>";
                }
                ?>
                </select>
    		</p>
            <p>        
    			<label for="<?php echo $this->get_field_id('kw');?>"><?php echo 'Keyword after store name'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('kw'); ?>" name="<?php	echo $this->get_field_name('kw');?>" value="<?php echo $instance['kw'];?>" style="width:100%;" />
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('number_show');?>"><?php echo 'Number of stores to show (default 5)';?></label>
    			<input type="text" id="<?php echo $this->get_field_id('number_show');?>" name="<?php echo $this->get_field_name('number_show');?>" value="<?php echo $instance['number_show'];?>" style="width:60%;" />
    		</p>
            <p>
                <label for="<?php echo $this->get_field_id('show_at'); ?>"><?php _e('Show at', ''); ?></label>
                <select name="<?php echo $this->get_field_name('show_at'); ?>" id="<?php echo $this->get_field_id('show_at') ?>" >
                <?php
                foreach ($arr_show_at as $k => $v) {
                    if ($instance['show_at'] == $k)
                        $selected = 'selected';
                    else
                        $selected = '';
                    echo "<option value='{$k}' {$selected}>$v</option>";
                }
                ?>
                </select>    
            </p>                 
        </div>
<?php
	   }
    }
?>
